package talk

public class User 
{

	String pseudo
	String firstName
	String lastName
	String password
	String email
	int age
	String accountGoogle = null
	String path;
	boolean url = false;
	
    static constraints = {
		pseudo(nullable: false, blank: false, unique: true)
		password(nullable: false, blank: false)
		firstName(nullable: true)
		lastName(nullable: true)
		email(nullable: true)
		age(nullable: true)
		accountGoogle(nullable: true)
		path(nullable: true, blank: true)
		accountGoogle(nullable: true, blank: true)
    }
	
	static hasMany = [users: User, talks : Talk]
	static belongsTo = [User]
	
	def listTalksHome()
	{
		def list = []
		
		for(User user in users)
		{
			def listTmp = Talk.findAll("from Talk as t where t.userSender = ?", [user])
			listTmp.each {
				 list.add(it)
			}
		}
		
		/* On rajoute aussi les tweets de l'utilisateur en question */
		def listTalkYou = Talk.findAll("from Talk as t where t.userSender = ?", [this])
			listTalkYou.each {
				 list.add(it)
			 }	
			
		
		return list
    }
	
	def addAbonnement(User userToFollow)
	{
		this.addToUsers(userToFollow).save(failOnError: true)
	}
	
	def removeAbonnement(User userToFollow)
	{
		this.removeFromUsers(userToFollow).save(failOnError: true)
	}
	
	def addTalk(Talk talkSend)
	{
		this.addToTalks(talkSend).save(failOnError: true)
	}
	
	def addProfilePicture(String path)
	{
		
	}
}